/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef RUNNINGENVELOPE_H
#define RUNNINGENVELOPE_H

#include "Envelope.h"

class RunningEnvelope
{
	const Envelope * envelope;
	int position;
	int next_point;
	int prev_point;

	bool key_off;
	bool sustaining;
public:
	RunningEnvelope();
	void reset();
	bool is_active()const;
	bool associate( const Envelope * envelope );
	void advance();
	int evaluate()const;
	void set_key_off();
	void set_position( int pos );
};

inline bool RunningEnvelope::associate( const Envelope * envelope )
{
	if( envelope && envelope->size() >= 2 )
		this->envelope = envelope;
	else
		this->envelope = 0;

	reset();

	return true;
}

inline bool RunningEnvelope::is_active()const
{
	if( !envelope )
		return false;

	return envelope->is_active();
}

inline void RunningEnvelope::reset()
{
	position = -1;
	next_point = 1;
	prev_point = 0;
	key_off = false;
	sustaining = false;
}

inline RunningEnvelope::RunningEnvelope()
:	envelope( 0 )
{
	reset();
}

#endif